create database COMPUTER
go
use COMPUTER
go

drop database COMPUTER
create table DM_SanPham
(
	MaDM int identity(1,1) primary key,
	TenDM nvarchar (30),
	Hinh varchar(50)
)

create table SanPham
(
	MaSP int identity(1,1) primary key,
	TenSP nvarchar(50),
	MaDM int,
	Gia int,
	MieuTa nvarchar(200),
	Hinh varchar(50)
)
create table CT_HoaDon
(
	MaSP int,
	MaHD int,
	SoLuong int,
	GiaBan int,
	constraint fk_MaSP_MaDM primary key (MaSP, MaHD)
)
UPDATE SanPham
SET ThanhTien=SoLuong*Gia


 create table HoaDon
 (
	MaHD int identity(1,1) primary key,
	NgayLap smalldatetime,
	NgayGiao smalldatetime,
	UserName varchar(30), 
 )
 
 create table KhachHang
 (
	UserName varchar(30) primary key,
	Pass varchar(30),
	HoTen nvarchar(50),
	GioiTinh bit default 1,
	DiaChi nvarchar(50),
	DienThoai int,
	Email varchar(50) unique
 )
 
 create table Adminn
 (
	Account varchar(30) primary key,
	Pass varchar(30)
 )

 create table QuangCao
 (
	MaQC int identity(1,1) primary key,
	TenQC nvarchar(50),
	Hinh varchar(50),
	Link varchar(100),
 )
 
 create table ThongTin
 (
	LoaiTT nvarchar(50) primary key,
	NoiDung nvarchar(500),
 )
 
 alter table CT_HoaDon
 add constraint fk_MaSP_MaSP foreign key (MaSP) references SanPham(MaSP),
 constraint fk_MaDM_MaDM foreign key (MaHD) references HoaDon(MaHD)
 
 alter table SanPham
 add constraint fk_Madm_Sanpham foreign key (MaDM) references DM_SanPham(MaDM) 
 
 alter table HoaDon
 add constraint fk_user_user foreign key(UserName) references KhachHang(UserName)
 
 select *from KhachHang